//
//  ST_GiftViewModel.swift
//  btvideo
//
//  Created by mac on 2020/6/28.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SVGAPlayer
import SDWebImage
import RxCocoa
import RxSwift

class ST_GiftViewModel {
    
    //    MARK: - Properties
    private(set) var data_array = [GiftPageModel]()
    
    /// 守护数据
    private(set) var dataArrayForGuide = [GiftPageModel]()
    
    /// 只有礼物数据
    private(set) var onlyGiftArray = [GiftPageModel]()
    
    
    fileprivate var tabArray = [BTGiftTabModel]()
    
    let tabTitleArray = ["送礼物","幸运盲盒","贵族专属","守护ta"]
    
}


extension ST_GiftViewModel{
    
    fileprivate func createCollectionData()  {
        
        var array = [GiftPageModel]()
        
        let pageSize = ST_GiftItemView.rowCount * ST_GiftItemView.columnCount
        
        for (tabIndex,item) in tabArray.enumerated(){
            
            let chunkedArray = item.array.chunked(into: pageSize)
            
            for (childIndex,childItemArray) in chunkedArray.enumerated(){
                
                let m = GiftPageModel()
                
                m.pageArray = childItemArray
                
                m.indexOfTab = tabIndex
                
                m.currentPageOfTab = childIndex
                
                m.pages = item.tabPages
                
                array.append(m)
            }
        }
        
        data_array = array
        
        onlyGiftArray = data_array.filter({ $0.indexOfTab == 0 })
        
        
//         提取守护数据
        
        guard let ordinaryGuide = tabArray.first(where: { $0.array.first?.tabType == .sweetGuard }) else{
            return
        }
        
        var arrayGuide = [GiftPageModel]()
        
        let tabIndex = 0
        
        let chunkedArray = ordinaryGuide.array.chunked(into: pageSize)
        
        for (childIndex,childItemArray) in chunkedArray.enumerated(){
            
            let m = GiftPageModel()
            
            m.pageArray = childItemArray
            
            m.indexOfTab = tabIndex
            
            m.currentPageOfTab = childIndex
            
            m.pages = ordinaryGuide.tabPages
            
            arrayGuide.append(m)
        }
        
        dataArrayForGuide = arrayGuide
        
        
    }
}

extension ST_GiftViewModel{
    
    func fetchGiftListSession(ignoreBlindBox:Bool = false)->Observable<Result<Void,ST_NetworkError>>{
        
        
        return TIANSession(requestUrl: PH.fetchGiftModuleBaseApi).withUnretained(self).flatMap { own,result -> Observable<Result<Void,ST_NetworkError>> in
            
            switch result {
            case .success(let json):
                
                let data = json["data"]
                
                print("礼物接口：",data)
                
                let nobilityTab = BTGiftTabModel()
                nobilityTab.title = own.tabTitleArray[2]
                nobilityTab.array = data["nobility"].arrayValue.map{ ST_BTGiftModel(json: $0, giftType: .gift) }
                
                
                let giftGuardTab = BTGiftTabModel()
                giftGuardTab.title = own.tabTitleArray[3]
                giftGuardTab.array = data["guard"].arrayValue.map{ ST_BTGiftModel(json: $0, giftType: .sweetGuard) }
                
                let normalGiftTab = BTGiftTabModel()
                normalGiftTab.title = own.tabTitleArray[0]
                normalGiftTab.array = data["goods"].arrayValue.map{ ST_BTGiftModel(json: $0, giftType: .gift) }
                
                if !ignoreBlindBox{
                    
                    let blindBoxTab = BTGiftTabModel()
                    blindBoxTab.title = own.tabTitleArray[1]
                    blindBoxTab.array = data["blind_box"].arrayValue.map{ ST_BTGiftModel(json: $0, giftType: .gift) }
                    
                    own.tabArray = [normalGiftTab,blindBoxTab,nobilityTab,giftGuardTab]
                    
                }else{
                    
                    own.tabArray = [normalGiftTab,nobilityTab,giftGuardTab]
                    
                }
                
                own.createCollectionData()
                
                return Observable.just(Result.success(()))
                
            case .failure(let e):
                
                return Observable.just(Result.failure(e))
            }
        }
    }
    
    func indexPathOf(tabIndex:Int) -> IndexPath {
        
        var row = 0
        if tabIndex < 1{
            return IndexPath.init(row: row, section: 0)
        }
        for i in 0..<tabIndex {
            
            if let tab = tabArray[safe:i]{
                
                row += tab.tabPages
            }
        }
        return IndexPath.init(row: row, section: 0)
    }
    
    
    func selectFirstGift()->ST_BTGiftModel?{
        
        let firstItem = data_array.first?.pageArray.first
        
        firstItem?.isSelected = true
        
        return firstItem
    }
    
    func selectFirstGuideGift()->ST_BTGiftModel?{
        
        let firstItem = dataArrayForGuide.first?.pageArray.first
        
        firstItem?.isSelected = true
        
        return firstItem
    }
    
    func selectFirstOnlyGift()->ST_BTGiftModel?{
        
        let firstItem = onlyGiftArray.first?.pageArray.first
        
        firstItem?.isSelected = true
        
        return firstItem
    }
}






class GiftPageModel: NSObject {
    
    
    /**
     第几个tab
     */
    var indexOfTab = 0
    /**
     当前Tab的第几页
     */
    var currentPageOfTab = 0
    
    /**
     总页数
     */
    var pages = 0
    
    var pageArray = [ST_BTGiftModel]()
}
